package com.block.source.service.receive;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.model.vo.ResultInfoVO;
import com.block.db.entity.MerchantBaseInfo;
import com.block.db.entity.SourceBaseInfo;
import com.block.db.entity.SourcePushResultInfo;
import com.block.source.model.dto.SourceApiParam;
import com.block.source.model.dto.SourceApiResult;
import com.block.source.model.param.UserDataParam;
import com.block.source.service.ISourceReceiveService;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class DefaultReceiveService extends ISourceReceiveService {

    @Override
    public SourceApiResult handleCheckUserData(SourceBaseInfo sourceBaseInfo, Map<String, String[]> parameterMap, String paramContent) {
        SourceApiResult sourceApiResult = new SourceApiResult();
        sourceApiResult.setSuccess(false);

//        JSONObject returnObject = new JSONObject();
//        returnObject.put("code", "500");
//        returnObject.put("message", "撞库失败");
//        sourceApiResult.setReturnObject(returnObject);

        UserDataParam userDataParam = this.getUserDataParam( paramContent);

        sourceApiResult.setUserDataParam(userDataParam);
        sourceApiResult.setSuccess(true);

        return sourceApiResult;
    }

    @Override
    public SourceApiResult handleRegisterUserData(SourceBaseInfo sourceBaseInfo, Map<String, String[]> parameterMap, String paramContent) {
        return null;
    }

    @Override
    public SourceApiResult getCheckUserReturnData(SourceBaseInfo sourceBaseInfo, ResultInfoVO resultInfoVO, SourceApiParam sourceApiParam) {

        SourceApiResult sourceApiResult = new SourceApiResult();

        MerchantBaseInfo merchantBaseInfo = sourceApiParam.getMerchantBaseInfo();
        SourcePushResultInfo pushResultInfo = sourceApiParam.getSourcePushResultInfo();

        JSONObject returnObject = new JSONObject();
        returnObject.put("code", "200");
        returnObject.put("message", "成功");

        JSONObject dataObject = new JSONObject();
        dataObject.put("productName", merchantBaseInfo.getInfoName());
        dataObject.put("companyName", merchantBaseInfo.getCompanyName());
        dataObject.put("companyLogo", merchantBaseInfo.getCompanyLogo());
        dataObject.put("maxLoanAmount", merchantBaseInfo.getMaxLoanAmount());
        dataObject.put("minLoanAmount", merchantBaseInfo.getMaxRate());

        returnObject.put("data", dataObject);

        sourceApiResult.setReturnObject(returnObject);

        return sourceApiResult;
    }

    @Override
    public SourceApiResult getRegisterUserReturnData(SourceBaseInfo sourceBaseInfo, ResultInfoVO resultInfoVO,SourceApiParam sourceApiParam) {
        SourceApiResult sourceApiResult = new SourceApiResult();

        JSONObject returnObject = new JSONObject();

        JSONObject data = resultInfoVO.getData();
        String respCode = resultInfoVO.getRespCode();

        if (StrUtil.equals(respCode, "200") && resultInfoVO.isSuccess()){
            String status = data.getString("status");
            if (StrUtil.equals(status, "000")){
                returnObject = this.getSuccess();
                sourceApiResult.setReturnObject(returnObject);
                return sourceApiResult;
            }
        }

        returnObject = this.getError("推送失败");
        sourceApiResult.setReturnObject(returnObject);

        return sourceApiResult;
    }

    @Override
    public JSONObject getError(String message) {
        JSONObject returnObject = new JSONObject();
        returnObject.put("code", "500");
        returnObject.put("message", message);
        return returnObject;
    }

    @Override
    public JSONObject getSuccess() {
        JSONObject returnObject = new JSONObject();
        returnObject.put("code", "200");
        returnObject.put("message", "成功");
        return returnObject;
    }

    @Override
    public UserDataParam getUserDataParam( String paramContent) {
        return JSON.parseObject(paramContent, UserDataParam.class);
    }



}
